<?php

class service_l_category
{

    protected $categoryTree;
    protected $site;
    public function __construct(){
        $this->site = service_l_core::get_current_site();
    }

     public static function save_category($array_data) {
        $c = new category_m_category();
        if($array_data['cat_id'] != "null")     // In case of Edit (Existing Category)
            $c = $c->get_by_id($array_data['cat_id']);

        $c->code = $array_data['code'];
        ////$c->publish = $array_data['publish'];
        $c->save();

        if($c->exists())
        {
            $cd = new desc_m_category();
            if($array_data['cat_id'] != "null")    // In case of Edit (Existing Category)
                $cd = $cd->get_by_category_id($array_data['cat_id']);
            $cd->category_id = $c->id;
            $cd->title = $array_data['title'];
            $cd->long_desc = $array_data['long_desc'];
            $cd->short_desc = $array_data['short_desc'];
            ////$cd->language_id = $array_data['language_id'];
            $cd->save();

            /*
             * In case of edit (cat_id != null), we dont need to add record into Table 'categories_association'.
             * parent_cat_id = null means, the Category we are going to save is a root Category.
             * For Root Category addition, we dont need to add record into Table 'categories_association'.
             */
            if($array_data['cat_id'] == "null" && $array_data['parent_cat_id']!="null")     
            {
                $ca = new association_m_category();
                $ca->parent_cat_id = $array_data['parent_cat_id'];
                $ca->cat_id = $c->id;
                $ca->save();
            }
        }

        return $c;
    }

    public static function delete_category($cat_id)
    {
        $c = new category_m_category();
        $c = $c->get_by_id($cat_id);
        
        $c->is_deleted = 1;
        return $c->save();
    }

   public function get_root_categories()
    {
        $objCategories = new category_m_category();
        return $objCategories->query("select * from categories where is_deleted = 0 and ID not in (
                                                        select distinct cat_id from categories_association where is_deleted = 0 ) order by sequence");
    }

    public function get_cat_path($cat_id, $for=NULL)
    {
        //@TO_DO : o something based on global or category module settins..based on the setting generate menu
        $site_url = $this->site->url."index.php/category/browsing/category";
        $objCategory = new category_m_category();
        $objCategory->where("is_deleted", "0")->where("id",$cat_id)->get();
        if($for=="cat_path")
            $cat_path = " ".$objCategory->get_desc()->title." ";
        else
            $cat_path = "<a href='".$site_url.'/'.$objCategory->id."/0'>".$objCategory->get_desc()->title."</a>";

        $cat_asso = new association_m_category();
        $cat_asso->where("cat_id", $cat_id);
        $cat_asso->where("is_deleted", "0");
        $cat_asso->get();

        if($cat_asso->exists())
        {
            $cat_path = $this->get_cat_path ($cat_asso->parent_cat_id, $for).">>>".$cat_path;
             return $cat_path;
        }
        else
            return $cat_path;
    }

    public static function get_next_level_child_categories($cat_id)
    {
        $objCategories = new category_m_category();
        return $objCategories->query("select * from Categories where is_deleted = 0 and ID in (
                                               select distinct cat_id from categories_association where is_deleted = 0  and Parent_Cat_Id = ".$cat_id.") order by sequence");
    }


    public function get_cat_tree_menu($type)
    {
        $rootCategories = $this->get_root_categories();

        if($type=='tree')
            $this->categoryTree = "<ul style='display: none;'>";
        else
            $this->categoryTree = "<ul>";

        foreach ($rootCategories as $cat)
            $this->build_list($cat, $type);

        $this->categoryTree = $this->categoryTree."</ul>";

        return $this->categoryTree;
    }

    private  function build_list($objCat, $type) 
    {
        //@TO_DO : o something based on global or category module settins..based on the setting generate menu
       $site_url = $this->site->url."index.php/category/browsing/category";
       if($type=='tree'){
            $this->categoryTree = $this->categoryTree."<li id='".$objCat->id."'>".$objCat->get_desc()->title;
       }
       else if($type=='prod_tree'){
            $this->categoryTree = $this->categoryTree."<li id='".$objCat->id."'>".$objCat->get_desc()->title." (".$this->total_products_till_last_child($objCat->id).")";
       }
       else{
           $this->categoryTree = $this->categoryTree."<li><a href='".$site_url."/".$objCat->id.'/0'."'>".$objCat->get_desc()->title." <span style='font-size:11px;color:gray'>($objCat->code)</span> </a>";
       }

       $cat_asso = new association_m_category();
       $cat_asso->where("parent_cat_id", $objCat->id);
       $cat_asso->where("is_deleted", "0");
       $sub_cats = $cat_asso->get();

        if($sub_cats->exists())
        {
            $this->categoryTree = $this->categoryTree."<ul>";

            foreach($sub_cats as $sub_cat)
            {
                $objCategory = new category_m_category();
                $objCategory->where("is_deleted", "0")->where("id",$sub_cat->get_child()->id)->get();
                if($objCategory->exists())
                    $this->build_list($sub_cat->get_child(), $type);
            }

            $this->categoryTree = $this->categoryTree."</ul>";
        }
        if($type=="menu")
            $this->categoryTree.="</li>";
    }
    
    public function get_products($cat_id,$page=1,$page_size=50){
        //@TO_DO : page size should come from category module settings
        $cat = new category_m_category();
        $cat->get_by_id($cat_id);
        $obj = $cat->products_m_category->get_paged($page,$page_size);
        return $obj;
    }

    public function total_products_till_last_child($cat_id)
    {
        $total_products = $this->get_products($cat_id)->count();

        $cat_assos = new association_m_category();
        $cat_assos->where("parent_cat_id", $cat_id);
        $cat_assos->where("is_deleted", "0");
        $cat_assos->get();

        if($cat_assos->exists())
            foreach($cat_assos as $cat_asso)
                $total_products = $total_products + $this->total_products_till_last_child ($cat_asso->cat_id);
             
        return $total_products;
    }

    public static function get_category($cat_id)
    {
        $c = new category_m_category();
        return $c->get_by_id($cat_id);
    }
}
?>